import os
import datetime
import smtplib
from email.mime.text import MIMEText
from email.header import Header
import requests
import logging as log
from contextlib import closing

def folder_exist(dir_path):
    '''
    1. 作用:判断文件夹路径是否存在,不存在则创建
    2. 参数:dir_path:文件夹路径
    3. 返回值:None
    '''
    if not os.path.exists(dir_path):
        os.makedirs(dir_path)
def DownloadFile(data_url, dir_name, data_name):
    # check_download_dir(folder_name)
    try:
        with closing(requests.get(data_url, stream=True)) as r:
            rc = r.status_code
            if 299 < rc or rc < 200:
                print('returnCode%s\t%s' % (rc, data_url))
                return
            content_length = int(r.headers.get('content-length', '0'))

            if content_length == 0:
                print('size0\t%s' % data_url)
                # return
            try:
                with open(os.path.join(dir_name, data_name), 'wb') as f:
                    for data in r.iter_content(1024):
                        f.write(data)
            except:
                # print('save fail \t%s' % data_url)
                log.error('save fail \t%s' % data_url, exc_info=True)
    except:
        # print('requests fail \t%s' % data_url)

        log.error('requests fail \t%s' % data_url, exc_info=True)

def file_readlines(file_path, mode='r', encoding='utf8'):
    '''
    1. 作用:逐行读取文件内容，并保存到列表中，列表的一个元素为文件一行的内容
    2. 参数：
        file_path:文件路径
        mode:文件读方式,默认为'r'
        encoding:文件编码,默认为'utf8'
    3. 返回值:逐行读取文件内容的列表
    '''
    f = open(file_path, mode=mode, encoding=encoding)
    file_content = f.readlines()
    f.close()
    return file_content

def get_list_file(path):
    '''
    1.作用:遍历文件夹下的所有指定后缀名的文件的具体路径
    2.参数:path:需要遍历的目录，
    3.返回值:文件的绝对路径
    '''
    list_file=[]
    for root, dirs, files in os.walk(path):
        for file in files:
            fpath = os.path.join(root, file)
            list_file.append(fpath)
    return list_file

def get_before_yesterday(current_time):
    '''
    1. 作用:获取前2天的年月日字符串
    2. 参数:str_time:时间字符串
    3. 返回值:前2天的字符串
    '''
    now_time = datetime.datetime.strptime(current_time, '%Y%m%d')
    before_yesterday = now_time + datetime.timedelta(days=-2)
    str_before_yesterday = datetime.datetime.strftime(before_yesterday, "%Y%m%d")
    return str_before_yesterday

def folder_exist(dir_path):
    '''
    1. 作用:判断文件夹路径是否存在,不存在则创建
    2. 参数:dir_path:文件夹路径
    3. 返回值:None
    '''
    if not os.path.exists(dir_path):
        os.makedirs(dir_path)

def get_yesterday(current_time):
    '''
    1. 作用:获取前一天的年月日字符串
    2. 参数:str_time:时间字符串
    3. 返回值:前一天的字符串
    '''
    now_time = datetime.datetime.strptime(current_time, '%Y%m%d')
    yesterday = now_time + datetime.timedelta(days=-1)
    str_yesterday = datetime.datetime.strftime(yesterday, "%Y%m%d")
    return str_yesterday

# 创建gfs文件
def create_text_gfs():
    filelist_path = 'gfs_filelist.txt'
    today = datetime.datetime.today().strftime('%Y%m%d')
    yestoday = get_yesterday(today)
    file_list = open(filelist_path).read().split('\n')
    for filename in file_list:
        middle_dir = filename[5:7]
        current_dir = os.path.join('gfs.'+yestoday, middle_dir)
        folder_exist(current_dir)
        file_path = os.path.join(current_dir, filename[:-1])
        with open(file_path, 'w') as f:
            f.write(' ')

# 检查gfs文件
def audit_gfsfile():
    today = datetime.datetime.today().strftime('%Y%m%d')
    # today = '20210428'
    before_yestoday = get_before_yesterday(today)
    current_dir = '/data/qixiang/gfs/gfs.' + before_yestoday
    gfs_complete_file = open('gfs_filelist.txt').read().split('\n')
    # print(gfs_complete_file)
    list_file = get_list_file(current_dir)
    # print(len(list_file))
    incomplete_url = []
    if len(list_file) == 524:
        return incomplete_url
    else:
        for filename in list_file:
            # 排除下载完成的文件
            print(filename.split('/')[-1])
            gfs_complete_file.remove(filename.split('/')[-1])

        # print(gfs_complete_file)
        for incomplete_file in gfs_complete_file:
            url = os.path.join('https://nomads.ncep.noaa.gov/pub/data/nccf/com/gfs/prod/', current_dir, incomplete_file[5:7], incomplete_file)
            incomplete_url.append(url)
    return incomplete_url

# 创建cfs文件
def create_text_cfs():
    list_hour = ['00', '06', '12', '18']
    today = datetime.datetime.today()
    str_day = today + datetime.timedelta(days=-2)
    str_day = datetime.datetime.strftime(str_day, "%Y%m%d")
    temp_dir = 'cfs.' + str_day
    folder_exist(temp_dir)
    current_time = datetime.datetime.strptime(str_day, '%Y%m%d')

    list_filename = []
    list_filename.append('flxf' + str_day + '12.01.' + str_day + '12.grb2')
    list_filename.append('flxf' + str_day + '18.01.' + str_day + '12.grb2')
    list_filename.append('pgbf' + str_day + '12.01.' + str_day + '12.grb2')
    list_filename.append('pgbf' + str_day + '18.01.' + str_day + '12.grb2')
    for i in range(1, 31):
        after_time = current_time + datetime.timedelta(days=+i)
        str_after_time = datetime.datetime.strftime(after_time, "%Y%m%d")

        for hour in list_hour:
            filename = 'flxf' + str_after_time + hour + '.01.' + str_day + '12.grb2'
            list_filename.append(filename)
            filename = 'pgbf' + str_after_time + hour + '.01.' + str_day + '12.grb2'
            list_filename.append(filename)
    for filename in list_filename:
        file_path = os.path.join(temp_dir ,filename)
        open(file_path, 'w').write('')


# 检查cfs文件
def audit_cfsfile():
    today = datetime.datetime.today()
    str_day = today + datetime.timedelta(days=-2)
    str_day = datetime.datetime.strftime(str_day, "%Y%m%d")
    temp_dir = '/data/qixiang/cfsv2/cfs.' + str_day
    file_length = len(get_list_file(temp_dir))
    if file_length == 244:
        return True
    else:
        return False

'''
class Mail():
    def __init__(self, mail_host, mail_user, mail_pass):
        self.smtp_object = smtplib.SMTP() # 创建smtp对象
        self.smtp_object.connect(mail_host, 25)    # 25 为 SMTP 端口号
        self.smtp_object.login(mail_user, mail_pass) # 登录邮箱
    
    # 发送邮件
    def send_mail(self, sender, receivers, title, content):
        #  sender:发件人, receivers:收件人, title:邮件标题, content:发送内容
        message = MIMEText(content, 'html', 'utf-8') # 内容
        message['From'] = Header("发件人", 'utf-8') # 显示发件人
        message['To'] =  Header("收件人", 'utf-8') # 显示收件人
        message['Subject'] = Header(title, 'utf-8') # 标题
        self.smtp_object.sendmail(sender, receivers, message.as_string()) # 发送邮件
    
    def __del__(self):
        self.smtp_object.quit()
'''
class Mail1():
    def __init__(self, mail_host, mail_user, mail_pass):
        self.smtp_object = smtplib.SMTP() # 创建smtp对象
        self.smtp_object.connect(mail_host, 25)    # 25 为 SMTP 端口号
        self.smtp_object.login(mail_user, mail_pass) # 登录邮箱

    # 发送邮件
    def send_mail(self, sender, receivers, title, content):
        '''
            sender:发件人
            receivers:收件人
            title:邮件标题
            content:发送内容
        '''
        message = MIMEText(content, 'plain', 'utf-8') # 内容
        message['From'] = sender # 显示发件人
        message['To'] =  receivers # 显示收件人
        message['Subject'] = title # 标题

        self.smtp_object.sendmail(sender, receivers.split(','), message.as_string()) # 发送邮件

    def __del__(self):
        self.smtp_object.quit()

if __name__ == '__main__':
    # create_text_gfs()
    # create_text_cfs()

    # mail_host = "smtp.163.com"  # 设置服务器
    # mail_user = "zhang22400228@163.com"    # 用户名
    # mail_pass = "zurenyou52240."   # 口令或密码
    # sender = 'zhang22400228@163.com' # 发送人
    # receivers = ['zhang22400228@163.com']  # 接收邮件，可在列表内填写多个
    # mail_object = Mail1(mail_host, mail_user, mail_pass)


    mail_host = 'smtp.em-data.com.cn'
    mail_user = 'zhangzuren@em-data.com.cn'
    mail_pass = 'zurenyou52240.'

    sender = '张祖仁<zhangzuren@em-data.com.cn>'
    receivers = '张祖仁<zhangzuren@em-data.com.cn>'
    mail_object = Mail1(mail_host, mail_user, mail_pass)


    # mail_host = 'smtp.em-data.com.cn'
    # mail_user = 'Afakerchen@em-data.com.cn'
    # mail_pass = 'asdf1234?'

    # sender = '陈学文<Afakerchen@em-data.com.cn>'
    # receivers = '陈学文<Afakerchen@em-data.com.cn>'
    # mail_object = Mail1(mail_host, mail_user, mail_pass)

    #title = 'gfs数据下载情况'
    incomplete_url = audit_gfsfile()
    state = 0
    url_list = []
    if bool(incomplete_url):
        title = 'gfs未下载完成，'
        mail_content = '<h2 color="red">gfs文件未下载完成，请及时下载！！！</h1><h3>未完成列表:</h3>'
        for url in incomplete_url:
            # mail_content = mail_content + '<h4><a href="' + url + '">' + url + '</a></h4>'
            # url_list.append(url)
            str1 = url.replace('/data/qixiang/gfs','https://ftp.ncep.noaa.gov/data/nccf/com/gfs/prod')
            list1 = list(str1)
            str2 = '/atmos'
            list1.insert(64,str2)
            file_url = ''.join(list1)
            mail_content = mail_content + '<h4><a href="' + file_url + '">' + file_url + '</a></h4>'
            url_list.append(file_url)


        state = 1
    else:
        title = 'gfs下载完成，'
        mail_content = '<h2>今天所有gfs文件都下载成功</h2>'

    cfs_result = audit_cfsfile()
    if cfs_result:
        title += 'cfs下载完成'
        mail_content = mail_content + '<h2>今天所有cfs文件都下载成功</h2>'
    else:
        title += 'cfs下载未完成'
        mail_content = mail_content + '<h2>cfs文件下载未完成，请及时下载！！！</h2>'
    mail_object.send_mail(sender, receivers, title, mail_content)
    if state == 1:
        for i in url_list:
            data_name = i.split('/')[-1]
            dir_name = i.replace(data_name,'').replace('https://ftp.ncep.noaa.gov/data/nccf/com/gfs/prod','/data/qixiang/gfs')
            folder_exist(dir_name)
            DownloadFile(i,dir_name,data_name)
        state = 0
